import * as c from '@/util/c';
import { log, info, warn, error } from "@/util/tio"
import * as tio from "@/util/tio"
import axios from 'axios';

import qs from "query-string";

/**
 * global options
 * @returns 
 */
export function gl_opt() {
    return {
        "baseURL": "/",//c.base,
        timeout: 50000,
        // headers: {'X-Custom-Header': 'foobar'}

    }
}

export const http = axios.create(gl_opt());

/**
 * 获取真实url 
 * @param url "/user/getbyid", "/user/getbyid?name=9"
 * @returns "/mytio/user/getbyid.tio_x", "/mytio/user/getbyid.tio_x?name=9"
 */
export function tiourl(url: string) {
    // return url;
    return _tiourl(url);
}

function _tiourl(url: string) {
    let ret = c.HTTP_API_PREFIX;
    let position = url.indexOf("?");

    if (position != -1) {
        let str1 = url.substring(0, position);
        let str2 = url.substring(position);
        // let u = url.split("?");
        ret += str1 + c.HTTP_API_SUFFIX + str2;
    } else {
        ret += url + c.HTTP_API_SUFFIX;
    }
    // log(url, ret);
    return ret;
}


/**
 * 
 * @param path "/user/getbyid"
 * @param method get or post
 * @param params {name:"talent.tan", id:99888}
 * @param isJson 是否以json格式发送数据，默认false
 * @param axiosOptions option for axios
 * @param stringifyOptions option for stringify
 * @returns 
 */
export const request = async (path, method, params?, isJson = false, axiosOptions?, stringifyOptions?) => {
    const opts = gl_opt();
    let _http = http
    if (axiosOptions) {
        Object.assign(opts, axiosOptions);
        _http = axios.create(opts);
    }
    let isGet = method == "get" || method == "GET"
    path = tiourl(path)
    if (!isJson && params && typeof params === 'object') {
        params = qs.stringify(params, stringifyOptions);
    }
    if (isGet) {
        if (!isJson) {  //
            if (params) {
                let s = "?"
                if (path.indexOf("?") != -1) {
                    s = "&"
                }
                path += s + params
            }
            return _http.get(path)
        } else {
            return _http.get(path, { params }) //axios的get，需要有key为params的参数，{params:{name='talent.tan', id=777888}}
        }
    } else {
        return _http.post(path, params)  //注意axios中get与post传递参数的区别
    }
}

/**
 * 
 * @param path "/user/getbyid"
 * @param params {name:"talent.tan", id:99888}
 * @param axiosOptions option for axios
 * @param stringifyOptions option for stringify
 * @returns 
 */
export const get = async (path, params?, axiosOptions?, stringifyOptions?) => {
    return request(path, 'get', params, false, axiosOptions, stringifyOptions)
}

/**
 * @param path "/user/getbyid"
 * @param params {name:"talent.tan", id:99888}
 * @param isJson 是否以json格式发送数据，默认false
 * @param axiosOptions option for axios
 * @param stringifyOptions option for stringify
 * @returns 
 */
export const post = async (path, params?, isJson = false, axiosOptions?, stringifyOptions?) => {
    return request(path, 'post', params, isJson, axiosOptions, stringifyOptions)
}

export default http 